
const {
    router,
    db_query
} = require('./config')
const axios = require('axios')
const token = require('../../App/token')
// 客户
router.get('/', async (ctx, next) => {
    //   await ctx.render('index', {
    //     title: 'Hello Koa 2!'
    //   })

    ctx.body = '99999'

})

router.get('/getlogin', async (ctx, next) => {
    let query = ctx.query
    console.log(query)
    let appid = "wx9a63b7904b52f669"
    let secret = "d76a09611ef4f1f084cc9108b9402e3c"
    let url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${query.code}&grant_type=authorization_code`
    let res = await axios.get(url)
    console.log(res.data,'999');
    let openid = res.data.openid
    let sql = `select * from Common_User where openid='${openid}'`
    console.log(sql,'999')
    let res1 = await db_query(sql)
    console.log(res1)
    if (res1=='') {
        let sql = `INSERT INTO Common_User VALUES(null,'${query.nickName}','${query.avatarUrl}','${openid}',null,null,null,0)`
        let res2 = await db_query(sql)
        console.log(res2)
    }
    let sql2 = `select * from Common_User where openid='${openid}'`
    let res3 = await db_query(sql2)
    let tokens = await token.getToken(res3[0].id, 'consumer')
    console.log(tokens,'token')
    ctx.body = {
        code: "200",
        msg: "获取成功",
        token:tokens,
        data: res3
    }
})


router.get('/string', async (ctx, next) => {
    ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
    ctx.body = {
        title: 'koa2 json'
    }
})

module.exports = router
